<?php

class Pagination {

    function paging($total, $per_page, $link) {
        $num_page = ceil($total / $per_page);
//        var_dump($num_page);die();
        if (!isset($_GET['page'])) {
            $current_page = 1;
        } else
            $current_page = $_GET['page'];


        if ($current_page > 1)
            echo '<a href="' . $link . '?page=' . ($current_page - 1) . '">[Trang trước]</a>';

        if ($current_page <= 5) {
            for ($i = 1; $i <= 10; $i++) {
                echo '<a href="' . $link . '?page=' . $i . '"> ' . $i . ' </a>';
            }
        } elseif ($current_page >= ($num_page - 5)) {
            for ($i = $num_page - 10; $i <= $num_page; $i++) {
                echo '<a href="' . $link . '?page=' . $i . '"> ' . $i . ' </a>';
            }
        } else {
            for ($i = $current_page - 4; $i <= $current_page + 5; $i++) {
                echo '<a href="' . $link . '?page=' . $i . '"> ' . $i . ' </a>';
            }
        }

        if ($current_page < $num_page) {
            echo '<a href="' . $link . '?page=' . ($current_page + 1) . '"> [Trang Sau] </a>';
        }
    }

}
?>

